package com.njworkorder.Service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.njworkorder.Entity.AdministrativePersonnel;
import com.njworkorder.Entity.Route;
import com.njworkorder.Entity.Section;
import com.njworkorder.Entity.Workspace;
import com.njworkorder.Service.SectionService;
import com.njworkorder.Mapper.SectionMapper;
import com.njworkorder.Utils.NoCode;
import com.njworkorder.VO.SectionListVo;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author iron
* @description 针对表【section】的数据库操作Service实现
* @createDate 2024-11-08 10:50:51
*/
@Service
public class SectionServiceImpl extends MPJBaseServiceImpl<SectionMapper, Section>
    implements SectionService{

    @Override
    public MPJLambdaWrapper<Section> getMPJWrapper() {
       return new MPJLambdaWrapper<Section>()
                .selectAll(Section.class)
                .leftJoin(Route.class,Route::getId, Section::getRouteId, ext ->
                        ext.selectAs(Route::getRouteName, SectionListVo::getRouteName))
                .leftJoin(Workspace.class,Workspace::getId, Section::getWorkspaceId, ext ->
                        ext.selectAs(Workspace::getWorkspaceName, SectionListVo::getWorkspaceName))
                .leftJoin(AdministrativePersonnel.class,AdministrativePersonnel::getId, Section::getCreateApId, ext ->
                        ext.selectAs(AdministrativePersonnel::getName, SectionListVo::getCreateApName));
    }

    @Override
    public String getUpToDateSectionNo() {

        LambdaQueryWrapper<Section> wrapper = new LambdaQueryWrapper<Section>()
                .apply("1=1")
                .orderByDesc(Section::getSectionNo).last("limit 1");
        List<Section> list = list(wrapper);

        String sectionNo = "000001";
        if (!list.isEmpty()) {
            String s = list.getFirst().getSectionNo().substring(6);
            sectionNo = NoCode.getNoCode(s);
        }
        return sectionNo;
    }
}




